<?php
/**
 * Created by PhpStorm.
 * User: Love
 * Date: 2018/8/4
 * Time: 16:05
 */

namespace App\Http\Controllers\Admin;


use App\Admin;
use App\Notice;
use App\Http\Controllers\Controller;
use App\Exceptions\DeleteException;
use App\News;

use Illuminate\Http\Request;

/**
 * @property Admin model
 */
class AdminController extends Controller
{
    public function __construct(Admin $model)
    {
        $this->model = $model;
    }

    public function info()
    {
        $user = $this->model->find(auth('admin')->id());
        return $this->success([
            'user_id'=>$user->id,
            'name'=>$user->name,
            'avatar'=>$user->avatar??'',
            'access'=>[]
        ]);
    }
    public function notices(Request $request)
    {
        $notice = new Notice;
        $notice -> title = $request -> input('title');
        $notice -> content = $request -> input('content');
        $notice -> times = date('Y-m-d',time());
        $notice -> zuoze = auth('admin')->user()->real_name;
        $notice -> save();
        return $this->success();
    }
    public function update(Request $request,$id)
    {
        $data = $request -> all();
        $data = ['title'=>'标题','content'=>'内容'];
        $n = Notice::where('id',$id)->update($data);
        if($n) {
            return $this->success($n);
        }else{
            return $this->failed(201,'修改失败');
        }
            
    }
    public function destroy($id)
    {
        $n = Notice::find($id);
        if($n == null){
            return $this->failed(203,'删除失败');
        }
        if($n -> delete()){
            return $this->success();
        }
    } 
    public function news(Request $request)
    {
        $new = new News;
        $image = $request->input('images');
        $arr = explode(',',$image[0]);
        $n = explode('"',$arr[1]);
        $new -> image = $n[3];
        $new -> cate = $request->input('cate');
        $new -> title = $request->input('title');
        $new -> content = $request->input('content');
        if($new -> save()) {
            return $this->success();
        }
    }
    // public function newsinfo()
    // {
    //     $data = News::all();
    //     return view('user',['data'=>$data]);
    // }   
   

    
}